import * as types from './actionTypes';
import Util from '../common/utils';
import { hex_sha1 } from '../common/sha1';

//获取图形验证码
export let getValidateCode = (key) => {

    let url = global.U + '/api/uc/uc/userRegistService/validateCode';

    return dispatch => {
        // 请求登录信息
        dispatch(fetchValidateCode());
        return Util.post(url, {'key': key}, (res) => {
            dispatch(receiveValidateCode(res.data));
        }, (err) => {
            dispatch(receiveValidateCode(''));
        })
    }
};

export let fetchValidateCode = () => {
	return {
		'type' : types.FETCH_VALIDATE_CODE,
	}
};

export let receiveValidateCode = (res) => {
	return {
		'type' : types.RECEIVE_VALIDATE_CODE,
		'validateCode' : res
	}
};



//给手机发送验证码
export let sendMessageForMobile = (para) => {

    let url = global.U + '/api/uc/uc/userRegistService/sendMessageForMobile';

    return dispatch => {
        // 请求登录信息
        dispatch(sendMessage());
        return Util.post(url, para, (res) => {
            dispatch(receiveMessage(res));
        }, (err) => {
            dispatch(receiveMessage({}));
        })
    }
};

export let sendMessage = () => {
    return {
        'type' : types.SEND_MESSAGE_MOBILE,
        'loading' : true,
    }
};

export let receiveMessage = (res) => {
    return {
        'type' : types.RECEIVE_MESSAGE_MOBILE,
        'res' : res,
        'loading' : false,
    }
};


//验证用户填写的验证码是否正确
export let checkValidateCode = (para) => {

    let url = global.U + '/api/uc/uc/userRegistService/validate';

    return dispatch => {
        dispatch(frechCheckValidateCode());
        return Util.post(url, para, (res) => {
            dispatch(receiveCheckValidateCode(res));
        }, (err) => {
            dispatch(receiveCheckValidateCode({}));
        })
    }
};

export let frechCheckValidateCode = () => {
    return {
        'type' : types.FRECH_CHECK_VALIDATECODE,
        'loading' : true,
    }
};

export let receiveCheckValidateCode = (res) => {
    return {
        'type' : types.RECEIVE_CHECK_VALIDATECODE,
        'res' : res,
        'loading' : false,
    }
};




//设置密码
export let setPassword = (para) => {

    let url = global.U + '/api/uc/uc/userRegistService/regist';

    para['password'] = hex_sha1(para['password']);

    return dispatch => {
        dispatch(frechSetPassword());

        console.log('设置密码参数');
        console.log(para);

        return Util.post(url, para, (res) => {
            dispatch(receiveSetPassword(res));
        }, (err) => {
            dispatch(receiveSetPassword({}));
        })
    }
};

export let frechSetPassword = () => {
    return {
        'type' : types.FRECH_SET_PASSWORD,
        'loading' : true,
    }
};

export let receiveSetPassword = (res) => {
    return {
        'type' : types.RECEIVE_SET_PASSWORD,
        'res' : res,
        'loading' : false,
    }
};

